package com.surfilter.modules.module.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.surfilter.modules.module.dao.ActionDao;
import com.surfilter.modules.module.dao.ModuleDao;
import com.surfilter.modules.module.model.ActionModel;
import com.surfilter.modules.module.model.ModuleModel;
import com.surfilter.modules.module.service.ModuleMrg;
import com.surfilter.platform.base.exception.CustomException;
import com.surfilter.platform.base.model.ActionPermission;
import com.surfilter.platform.base.model.Constants;
import com.surfilter.platform.base.model.ParameterClassType;
import com.surfilter.platform.base.model.ParameterInfo;
import com.surfilter.platform.base.model.ParameterMapper;
import com.surfilter.platform.base.model.PermissionMethodType;
import com.surfilter.platform.base.service.ServiceBase;
import com.surfilter.platform.base.service.ServiceContext;

@Service(value = Constants.SERVICE_MODULE)
public class ModuleMrgImpl extends ServiceBase implements ModuleMrg {

	@Resource
	private ModuleDao moduleDao;
	@Resource
	private ActionDao actionDao;

	private static final int MODULE_AVALID = 1;
	private static final int MODULE_INVALID = 0;

	private static final int TYPE_TOC = 1;
	private static final int TYPE_FUN = 2;

	/*********************************** MODULE_START ******************************************/

	@ActionPermission(actionName = "getAllValidModules",remark = "获取所有合法模块", permission = PermissionMethodType.MODULE)
	public List<ModuleModel> getAllValidModules() {
		return moduleDao.getAllValidModules();
	}

	@ActionPermission(actionName = "getAllModules",remark = "获取所有模块", permission = PermissionMethodType.MODULE)
	public List<ModuleModel> getAllModules() {
		return moduleDao.getAllModules();
	}

	@ActionPermission(actionName = "addModule",remark = "添加模块", permission = PermissionMethodType.MODULE, ref = { "getModuleIdByName" })
	public ModuleModel addModule(ModuleModel moduleModel,
			HttpServletRequest request) {
		int sessionUserId = ServiceContext.getUserIdInSession(request);
		moduleModel.setCreateUser(sessionUserId);
		moduleModel.setCreateTime(new Date());
		int key = moduleDao.addModule(moduleModel);
		moduleModel.setId(key);
		return moduleModel;
	}

	@ActionPermission(actionName = "getModuleIdByName",remark = "根据模块名获取模块ID", permission = PermissionMethodType.MODULE)
	public ModuleModel getModuleIdByName(ModuleModel moduleModel,
			HttpServletRequest request) {
		ModuleModel model = moduleDao.getModuleByName(moduleModel
				.getModuleName());
		return model;
	}

	@ActionPermission(actionName = "chgModule",remark = "修改模块", permission = PermissionMethodType.MODULE)
	public int chgModule(ModuleModel moduleModel, HttpServletRequest request) {
		int sessionUserId = ServiceContext.getUserIdInSession(request);
		moduleModel.setUpdateUser(sessionUserId);
		moduleModel.setUpdateTime(new Date());
		int num = moduleDao.chgModule(moduleModel);
		return num;
	}

	@ActionPermission(actionName = "delModule",remark = "删除模块", permission = PermissionMethodType.MODULE)
	public int delModule(ModuleModel moduleModel) {
		int moduleType = moduleModel.getModuleType();
		int moduleId = moduleModel.getId();
		String moduleName = moduleModel.getModuleName();
		if (moduleType == TYPE_TOC) {
			int number = moduleDao.getChildrenNumber(moduleName);
			if (number > 0) {
				throw new CustomException("该目录下包含子模块，不能删除");
			}
		}

		int num = moduleDao.delModule(moduleModel);
		return num;
	}

	@ActionPermission(actionName = "startModule",remark = "启用模块", permission = PermissionMethodType.MODULE)
	public int startModule(ModuleModel moduleModel, HttpServletRequest request) {
		if (moduleModel.getValidFlag() == MODULE_AVALID) {
			throw new CustomException("该模块已启用，不能重复启用");
		}
		int sessionUserId = ServiceContext.getUserIdInSession(request);
		Date updateTime = new Date();
		int moduleId = moduleModel.getId();
		String pname = moduleModel.getPname();
		int valid = moduleModel.getValidFlag();

		List<Integer> startModuleIds = new ArrayList();
		startModuleIds.add(moduleId);

		while (true) {
			if ("root".equals(pname) || valid == MODULE_AVALID)
				break;
			ModuleModel model = moduleDao.getModuleByName(pname);
			startModuleIds.add(model.getId());
			pname = model.getPname();
			valid = model.getValidFlag();
		}
		int num = moduleDao.startModule(startModuleIds, sessionUserId,
				updateTime);
		return num;
	}

	@ActionPermission(actionName = "stopModule",remark = "停用模块", permission = PermissionMethodType.MODULE)
	public int stopModule(ModuleModel moduleModel, HttpServletRequest request) {
		if (moduleModel.getValidFlag() == MODULE_INVALID) {
			throw new CustomException("该模块已停用，不能重复停用");
		}
		if (moduleModel.getId() <= 1) {
			throw new CustomException("根模块不允许停用");
		}
		int sessionUserId = ServiceContext.getUserIdInSession(request);

		Date updateTime = new Date();

		String moduleName = moduleModel.getModuleName();

		List<ModuleModel> models = new ArrayList<ModuleModel>();
		models.add(moduleModel);
		this.setAllChildrenToList(models, moduleName);

		List<Integer> stopModuleIds = new ArrayList();

		for (ModuleModel model : models) {
			if (model.getValidFlag() == MODULE_INVALID)
				continue;
			stopModuleIds.add(model.getId());
		}

		int num = moduleDao
				.stopModule(stopModuleIds, sessionUserId, updateTime);
		return num;
	}

	private void setAllChildrenToList(List<ModuleModel> models, String pname) {
		List<ModuleModel> cModels = moduleDao.getAllChildrenByParent(pname);
		if (cModels == null || cModels.size() == 0)
			return;
		models.addAll(cModels);
		for (ModuleModel cModel : cModels) {
			if (cModel.getModuleType() == TYPE_TOC) {
				String mid = cModel.getPname();
				setAllChildrenToList(models, mid);
			}
		}
	}

	/*********************************** MODULE_END ******************************************/

	/*********************************** ACTION_START ******************************************/

	@ActionPermission(actionName = "getActionsOfModule",remark = "获取模块下所有action", permission = PermissionMethodType.MODULE)
	@ParameterMapper(@ParameterInfo(mapName = "moduleId", componentType = int.class, index = 0))
	public List<ActionModel> getActionsOfModule(int moduleId) {
		List<ActionModel> models = actionDao.getActionsOfModule(moduleId);
		return models;
	}

	@ActionPermission(actionName = "saveActions",remark = "保存action", permission = PermissionMethodType.MODULE)
	@ParameterMapper({
			@ParameterInfo(mapName = "actions", componentType = ActionModel.class, index = 0, mapType = ParameterClassType.JSONARRAY),
			@ParameterInfo(mapName = "delActionIds", componentType = Integer.class, index = 1, mapType = ParameterClassType.ARRAY) })
	public boolean saveActions(List<ActionModel> models,
			List<Integer> delActionIds, HttpServletRequest request) {
		int sessionUserId = ServiceContext.getUserIdInSession(request);

		Date updateTime = new Date();
		if (models != null && models.size() > 0) {
			for (ActionModel model : models) {
				int id = model.getId();
				model.setUpdateTime(updateTime);
				model.setUpdateUser(sessionUserId);
				if (id <= 0) {
					model.setCreateTime(updateTime);
					model.setCreateUser(sessionUserId);
					actionDao.addAction(model);
				} else {
					actionDao.chgAction(model);
				}
			}
		}
		if (delActionIds != null && delActionIds.size() > 0) {
			actionDao.delActions(delActionIds);
		}
		return true;
	}

	/*********************************** MODULE_END ******************************************/

}
